package com.ldk.admin.controller;

import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ldk.admin.common.LigerUIGridResult;
import com.ldk.base.framework.common.CustomPageable;
import com.ldk.base.framework.util.SystemUtil;
import com.ldk.framework.entity.PlatformFundFlowRecord;
import com.ldk.framework.service.IPlatformFundFlowRecordService;

@Controller
@RequestMapping("/fundflow")
public class FundFlowController extends BaseController {
	/**
	 * 路由地址
	 */
	private final String route = "fundflow";
	/**
	 * 模块名称
	 */
	private final String module = "平台资金流动记录";
	
	@Autowired
	private IPlatformFundFlowRecordService platformFundFlowRecordService;
	
	@RequestMapping("/list")
	//public ModelAndView list(ModelMap map){
	public ModelAndView list(ModelMap map,HttpServletRequest request,
							 HttpServletResponse response){
		//session是否过期
		this.isExpired(request,response);
		setLinks(map, "", "", module, "/"+route+"/list", "", "");
		
		Calendar calendar = Calendar.getInstance();
		String endTime = SystemUtil.getDateString(calendar.getTime(), "yyyy-MM-dd");
		
		calendar.add(Calendar.DAY_OF_MONTH, -2);
		String startTime = SystemUtil.getDateString(calendar.getTime(), "yyyy-MM-dd");
		map.addAttribute("route", route);
		map.addAttribute("module", module);
		map.addAttribute("startTime", startTime);
		map.addAttribute("endTime", endTime);

		//操作日志
		//this.insertOplog(request,module,"","","查看",this.getAdminUserName(request).toString(), Long.parseLong(this.getAdminId(request).toString()),SystemUtil.getAllSeconds());
		
		return new ModelAndView("/fundflow/list");
	}
	
	@ResponseBody
	@RequestMapping("/getList")
	public LigerUIGridResult<PlatformFundFlowRecord> getList(@RequestParam(value="page",required=false,defaultValue="1") int page, 
									         @RequestParam(value="pagesize",required=false,defaultValue="10") int pageSize,
									         @RequestParam(value="searchKey",required=false,defaultValue="") String searchKey,
									         HttpServletRequest request)
	{
		int limit = pageSize;
		int offset = (page -1) * pageSize;
		
		PlatformFundFlowRecord query = new PlatformFundFlowRecord();
		query.setSearchKey(searchKey);
		
		Sort sort = new Sort(Direction.DESC, "record_no");
		CustomPageable pageable = new CustomPageable(offset, limit, sort);
		Page<PlatformFundFlowRecord> result = platformFundFlowRecordService.selectPageList("", query, pageable);
		
		LigerUIGridResult<PlatformFundFlowRecord> grid = new LigerUIGridResult<>();
		grid.Total = result.getTotalElements();
		grid.Rows = result.getContent();
		
		return grid;
	}
}
